package com.littcore.saap.message.web;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.littcore.common.BeanManager;
import com.littcore.common.Utility;
import com.littcore.dao.page.IPageList;
import com.littcore.dao.ql.PageParam;
import com.littcore.exception.NotLoginException;
import com.littcore.module.annotation.Func;
import com.littcore.saap.common.vo.LoginUserVo;
import com.littcore.saap.core.web.util.LoginUtils;
import com.littcore.saap.message.biz.IUserMessageBizService;
import com.littcore.saap.message.bo.UserMessageBo;
import com.littcore.saap.message.vo.UserMessageStatVo;
import com.littcore.shield.vo.ILoginVo;
import com.littcore.web.util.WebUtils;


/**
 * .
 * 
 * <pre><b>描述：</b>
 *    
 * </pre>
 * 
 * <pre><b>修改记录：</b>
 *    
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">蔡源</a>
 * @since 2015年11月4日
 * @version 1.0
 */
@Controller
public class UserMessageController {
  
  @Resource
  private IUserMessageBizService userMessageBizService;
  
  @Func(moduleCode="personal.userMessage", funcCode="query", enableLog=false)
  @RequestMapping 
  public ModelAndView my(WebRequest request, ModelMap modelMap) throws NotLoginException
  { 
    LoginUserVo loginUser = (LoginUserVo)LoginUtils.getLoginVo();
            
    return new ModelAndView("/message/userMessage/my");  
  
  }   
  
  @RequestMapping 
  public void read(@RequestParam Integer id)
  {
    userMessageBizService.doRead(id);
  }
  
  @RequestMapping 
  public void delete(@RequestParam Integer id)
  {
    userMessageBizService.delete(id);
  }
  
  
  /**
   * 获得当前登录用户未读消息统计.
   * @return
   */
  @RequestMapping 
  public ModelAndView getUnreadCount()
  {
    ILoginVo loginUser = LoginUtils.getLoginVo();
    int tenantId = LoginUtils.getTenantId();
    Locale locale = loginUser.toLocale();
    
    List<UserMessageStatVo> userMessageStats = userMessageBizService.countUnread(loginUser.getOpId().intValue(), tenantId);
    int totalCount = 0;
    for (UserMessageStatVo vo : userMessageStats)
    {
      totalCount += vo.getUnreadCount();
      
      String moduleName = BeanManager.getMessage("message.module."+vo.getModuleCode(), locale);
      String title = BeanManager.getMessage("userMessageStat.title", new Object[]{vo.getUnreadCount(), moduleName}, locale);
      vo.addI18n("moduleName", moduleName);
      vo.addI18n("title", title);
    }
    
    return new ModelAndView("jsonView").addObject("userMessageStats", userMessageStats).addObject("totalCount", totalCount);
  }
  
  @RequestMapping 
  public ModelAndView getUnreadMessages()
  {
    ILoginVo loginUser = LoginUtils.getLoginVo();
    int tenantId = LoginUtils.getTenantId();
    Locale locale = loginUser.toLocale();
    
    List<UserMessageBo> unreadMessages = userMessageBizService.findUnread(loginUser.getOpId().intValue(), tenantId);
    
    return new ModelAndView("jsonView").addObject("unreadMessages", unreadMessages);
  }
  
  
  
}
